package pers.mihao.quickstream.account.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import org.springframework.stereotype.Repository;
import pers.mihao.quickstream.account.model.dto.AccountDTO;
import pers.mihao.quickstream.account.repository.AccountRepository;
import pers.mihao.quickstream.account.repository.convertor.AccountConvertor;
import pers.mihao.quickstream.account.repository.domain.AccountDO;
import pers.mihao.quickstream.account.repository.mapper.AccountMapper;
import pers.mihao.quickstream.common.model.ApiPageDTO;
import pers.mihao.quickstream.common.util.CollectionUtil;
import pers.mihao.quickstream.common.util.Query;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mihao
 * @since 2024/11/12
 */
@Repository
public class AccountRepositoryImpl implements AccountRepository {

    @Resource
    AccountConvertor accountConvertor;

    @Resource
    AccountMapper accountMapper;

    @Override
    public AccountDTO queryById(Long id) {
        return accountConvertor.map2AccountDTO(accountMapper.selectById(id));
    }

    @Override
    public List<AccountDTO> queryAccountByUserIdWithPage(ApiPageDTO apiPageDTO) {
        PageDTO<AccountDO> pageDTO = new PageDTO<>(apiPageDTO.getPageStart(), apiPageDTO.getPageSize());
        QueryWrapper<AccountDO> queryWrapper = new Query<AccountDO>().eq("sourceUserId", String.valueOf(apiPageDTO.getUserId()));
        List<AccountDO> accountDOS = accountMapper.selectList(pageDTO, queryWrapper);
        if (CollectionUtil.isEmpty(accountDOS)) {
            return new ArrayList<>();
        }
        return accountDOS.stream().map(accountConvertor::map2AccountDTO).collect(Collectors.toList());
    }
}
